package com.kingtone.rongji.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.kingtone.rongji.MainActivity;
import com.kingtone.rongji.ProductDetailActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.Product;
import com.kingtone.rongji.tool.AppRestClient;
import com.kingtone.rongji.tool.CartUtils;
import com.kingtone.rongji.ui.ToastManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

public class ProductGirdAdapter extends BaseAdapter {

	private LayoutInflater mInflater = null;

	private List<Product> mProducts = null;

	private DisplayImageOptions mOptions;

	private MainActivity mActivity;

	public ProductGirdAdapter(MainActivity activity, List<Product> products) {
		mProducts = products;
		this.mActivity = activity;
		mInflater = (LayoutInflater) activity.getApplicationContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mOptions = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.product_loading_img)
				.cacheInMemory(true).cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565).delayBeforeLoading(100)
				.build();
	}

	public int getCount() {
		return mProducts == null ? 0 : mProducts.size();
	}

	public Object getItem(int position) {
		return mProducts == null ? null : mProducts.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder holder = null;
		if (convertView != null) {
			view = convertView;
			holder = (ViewHolder) view.getTag();
		} else {
			view = mInflater.inflate(R.layout.product_grid_item, parent, false);
			holder = new ViewHolder();
		}

		holder.imageView = (ImageView) view.findViewById(R.id.id_img_product);
		holder.name = (TextView) view.findViewById(R.id.id_tv_name);
		holder.price = (TextView) view.findViewById(R.id.id_tv_price);
		holder.prompt = (FrameLayout) view.findViewById(R.id.id_product_prompt);
		holder.promptIcon = (ImageView) view
				.findViewById(R.id.id_img_product_prompt);
		holder.promptText = (TextView) view
				.findViewById(R.id.id_tv_product_prompt);
		holder.bagIcon = (ImageView) view.findViewById(R.id.id_img_bag);
		view.setTag(holder);

		final Product product = (Product) getItem(position);
		if (product == null) {
			return convertView;
		}

		if (product.isValidate()) {
			holder.bagIcon.setBackgroundResource(R.drawable.bag_deactive);
			holder.bagIcon.setOnClickListener(null);
		} else {
			holder.bagIcon.setBackgroundResource(R.drawable.bag_active);
			holder.bagIcon.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View current) {
					// TODO Auto-generated method stub
					Context context = current.getContext();
					CartUtils.add2Cart(context, product);
					ToastManager.show(context, current.getResources()
							.getString(R.string.add_to_cart_success));
					mActivity.updateBadge(1, true);
				}
			});
		}

		int promotionType = product.getPromotionType();
		switch (promotionType) {
		case 67:
			holder.prompt.setVisibility(View.VISIBLE);
			holder.promptText.setText("清");
			break;
		case 70:
			holder.prompt.setVisibility(View.VISIBLE);
			holder.promptText.setText("免");
			break;
		case 78:
			holder.prompt.setVisibility(View.VISIBLE);
			holder.promptText.setText("新");
			break;
		case 83:
			holder.prompt.setVisibility(View.VISIBLE);
			holder.promptText.setText("减");
			break;
		default:
			holder.prompt.setVisibility(View.GONE);
			break;
		}

		String tagUrl =  (String) holder.imageView.getTag();
		String url = AppRestClient.convertImageUrl(product.getProductId(),
				product.getImageName());
		holder.imageView.setTag(url);
		if ((tagUrl == null && url != null) || (url != null && !url.equals(tagUrl))) {
			ImageLoader.getInstance().displayImage(url, holder.imageView, mOptions);
		}
		if (holder.name != null) {
			holder.name.setText(product.getProductName());
		}
		holder.price.setText("￥" + product.getUnitPrice());

		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(view.getContext(),
						ProductDetailActivity.class);
				intent.putExtra("productId", product.getProductId());
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				view.getContext().startActivity(intent);
			}
		});

		return view;
	}

	private class ViewHolder {
		public ImageView imageView;
		public TextView name;
		public TextView price;
		public FrameLayout prompt;
		public ImageView promptIcon;
		public TextView promptText;
		public ImageView bagIcon;
	}
}
